﻿using UnityEngine;
using GameFramework;

namespace MYSTARFORCE
{
    public class Asteroid : TargetableObject
    {
        [SerializeField]
        private AsteroidData m_AsteroidData = null;

        // 自旋转角度
        private Vector3 m_RotateSphere = Vector3.zero;

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);

            m_AsteroidData = (AsteroidData)userData;
            if(m_AsteroidData == null)
            {
                Log.Warning("AsteroidData is invalied");
                return;
            }

            // 当启动的时候就旋转小行星的角度
            m_RotateSphere = Random.insideUnitSphere;

        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            // 让小行星向下移动
            CachedTransform.Translate(Vector3.back * m_AsteroidData.Speed * elapseSeconds,Space.World);

            // 让小行星旋转
            CachedTransform.Rotate(m_RotateSphere * m_AsteroidData.AngularSpeed * elapseSeconds, Space.Self);
        }

        protected override void OnDead(Entity attacker)
        {
            base.OnDead(attacker);

            // 播放死亡时的特效
            EffectData deadEffect = new EffectData(GameEntry.Entity.GenerateSerialId(), m_AsteroidData.DeadEffectId)
            {
                Position = CachedTransform.localPosition
            };
            GameEntry.Entity.ShowEffect(deadEffect);

            GameEntry.Sound.PlaySound(m_AsteroidData.DeadSoundId);
        }

        public override ImpactData GetImpactData()
        {
            return new ImpactData(m_AsteroidData.Camp, m_AsteroidData.HP, m_AsteroidData.Attack, m_AsteroidData.Defense);
        }
    }
}